/**
 * @description  : 检查勘验笔录快办-物品item编辑组件
 * @descriptionDetail : 检查勘验笔录快办-物品item编辑组件
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2023-01-04 19:03:02
 */
<template>
  <view class="page">
    <theme-navbar
      :is-white="false"
      :isBack="false"
      backColor="#2b85e4"
      title="品规编辑"
    >
    </theme-navbar>
    <view class="content">
      <u-form ref="uDcarForm" :model="dcarItemform" label-width="auto" label-position="left" :error-type="['border-bottom','message',]" :rules="rules" class="sform-content">
        <u-form-item label="品牌(规格)">
          <u-input input-align="right" v-model="dcarItemform.goodsName" :disabled="true" placeholder=""></u-input>
        </u-form-item>
        <u-form-item label="物品性质">
          <text style="width: 100%;text-align: right;">
            {{ getDicName($u.dic.DIC_KEYS.GOODS_PROP_SUBTYPE,dcarItemform.propSubtype,dcarItemform.propSubTypeName) }}
          </text>
        </u-form-item>
        <u-form-item label="进货数量">
          <u-input input-align="right" v-model="dcarItemform.qty" @input="inputQtyEvent" type="digit" :disabled="!edit" placeholder="请输入进货数量"></u-input>
        </u-form-item>
        <u-form-item label="进货单价">
          <u-input input-align="right" v-model="dcarItemform.buyPrice" @input="inputPriceEvent" type="digit" :disabled="!edit" placeholder="请输入进货单价"></u-input>
        </u-form-item>
        <u-form-item label="进货额">
          <u-input input-align="right" v-model="dcarItemform.amt" :disabled="true" placeholder=""></u-input>
        </u-form-item>
        <u-form-item label="销售数量">
          <u-input input-align="right" v-model="dcarItemform.illegalQty" @input="inputIQtyEvent" type="digit" :disabled="!edit" placeholder="请输入销售数量"></u-input>
        </u-form-item>
        <u-form-item label="销售单价">
          <u-input input-align="right" v-model="dcarItemform.illegalPrice" @input="inputIPriceEvent" type="digit" :disabled="!edit" placeholder="请输入销售单价"></u-input>
        </u-form-item>
        <u-form-item label="销售额">
          <u-input input-align="right" v-model="dcarItemform.illegalAmt" :disabled="true" placeholder=""></u-input>
        </u-form-item>
        <u-form-item label="获利">
          <u-input input-align="right" v-model="dcarItemform.profit" :disabled="true" placeholder=""></u-input>
        </u-form-item>
      </u-form>
      <view class="popup-submit-btns" v-if="edit">
        <u-button class="u-btn" plain type="error" @click="del">
          删除
        </u-button>
        <u-button class="u-btn" type="primary" @click="onSure">
          保存
        </u-button>
      </view>
    </view>
  </view>
</template>

<script>
import caseService from "@/service/case/case.service.js";
import { v4 as uuidv4, } from 'uuid';
export default{
  name:"DcarItemUpdate",
  props:{
    edit:{
      type:Boolean,
      default:false,
    },
    item:{
      type:Object,
      default:() => { return {} },
    },
  },
  data(){
    return {
      dcarItemform:{},
      rules:[],
    }
  },
  created() {
    this.dcarItemform = this.$u.deepClone(this.item);
  },
  methods:{
    getDicName(keyword, code, defaultName) {
      if(defaultName) return defaultName;
      return this.$u.dic.getDicNameByKeyCode(keyword, code);
    },
    updateProfit(){
      try{
        let jPrice = isNaN(Number(this.dcarItemform.buyPrice)) ? 0 : Number(this.dcarItemform.buyPrice);
        let sPrice = isNaN(Number(this.dcarItemform.illegalPrice)) ? 0 : Number(this.dcarItemform.illegalPrice);
        let sQty = isNaN(Number(this.dcarItemform.illegalQty)) ? 0 : Number(this.dcarItemform.illegalQty);
        let jsp = (sPrice - jPrice) > 0 ? (sPrice - jPrice) : 0;
        this.dcarItemform.profit = isNaN(Number(jsp * sQty)) ? 0 : Number(jsp * sQty);
      }catch(e2){
        this.dcarItemform.profit = 0;
      }
    },
    inputQtyEvent(e){
      try{
        let sQty = isNaN(Number(this.dcarItemform.qty)) ? 0 : Number(this.dcarItemform.qty);
        let sPrice = isNaN(Number(this.dcarItemform.buyPrice)) ? 0 : Number(this.dcarItemform.buyPrice);
        this.dcarItemform.amt = Number(sQty * sPrice).toFixed(2);
      }catch(e1){
        this.dcarItemform.amt = 0;
      }
      this.updateProfit();
    },
    inputPriceEvent(e){
      try{
        let sQty = isNaN(Number(this.dcarItemform.qty)) ? 0 : Number(this.dcarItemform.qty);
        let sPrice = isNaN(Number(this.dcarItemform.buyPrice)) ? 0 : Number(this.dcarItemform.buyPrice);
        this.dcarItemform.amt = Number(sQty * sPrice).toFixed(2);
      }catch(e1){
        this.dcarItemform.amt = 0;
      }
      this.updateProfit();
    },
    inputIQtyEvent(e){
      try{
        let sQty = isNaN(Number(this.dcarItemform.illegalQty)) ? 0 : Number(this.dcarItemform.illegalQty);
        let sPrice = isNaN(Number(this.dcarItemform.illegalPrice)) ? 0 : Number(this.dcarItemform.illegalPrice);
        this.dcarItemform.illegalAmt = Number(sQty * sPrice).toFixed(2);
      }catch(e1){
        this.dcarItemform.illegalAmt = 0;
      }
      this.updateProfit();
    },
    inputIPriceEvent(e){
      try{
        let sQty = isNaN(Number(this.dcarItemform.illegalQty)) ? 0 : Number(this.dcarItemform.illegalQty);
        let sPrice = isNaN(Number(this.dcarItemform.illegalPrice)) ? 0 : Number(this.dcarItemform.illegalPrice);
        this.dcarItemform.illegalAmt = Number(sQty * sPrice).toFixed(2);
      }catch(e1){
        this.dcarItemform.illegalAmt = 0;
      }
      this.updateProfit();
    },
    onSure(){
      if(this.$u.test.isEmpty(`${this.dcarItemform.qty}`) ||
		this.$u.test.isEmpty(`${this.dcarItemform.buyPrice}`) ||
		this.$u.test.isEmpty(`${this.dcarItemform.illegalQty}`) ||
		this.$u.test.isEmpty(`${this.dcarItemform.illegalPrice}`)
      ){
        this.$u.toast('请输入数量和单价');
        return;
      }
      this.$emit("ok", this.dcarItemform);
    },
    del(){
      this.$emit("del", this.dcarItemform);
    },
  },
}
</script>

<style scoped lang="scss">
	.content{
		padding: 20rpx;
	}
	.sform-content{
		padding: 10rpx 20rpx;
	}
	.popup-submit-btns{
		width: calc(100% - 22px);
		position: fixed;
		bottom: 10rpx;
	}
	.tp-class{
		width: 100%;
	}
</style>